package cn.edu.sjtu.se.peakfactory.service.impls;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.edu.sjtu.se.lchang.socialnetwork.dao.bean.Tag;
import cn.edu.sjtu.se.lchang.socialnetwork.dao.bean.User;
import cn.edu.sjtu.se.lchang.socialnetwork.dao.interfaces.TagDAO;
import cn.edu.sjtu.se.peakfactory.dao.bean.Game;
import cn.edu.sjtu.se.peakfactory.dao.bean.GameProfile;
import cn.edu.sjtu.se.peakfactory.dao.interfaces.GameDAO;
import cn.edu.sjtu.se.peakfactory.dao.interfaces.GameProfileDAO;
import cn.edu.sjtu.se.peakfactory.service.bean.GameVB;
import cn.edu.sjtu.se.peakfactory.service.interfaces.GameService;

public class GameServiceImpl implements GameService {
	private GameDAO gameDAO;
	private GameProfileDAO gameProfileDAO;
	private TagDAO tagDAO;

	public void setGameDAO(GameDAO gameDAO) {
		this.gameDAO = gameDAO;
	}

	public void setGameProfileDAO(GameProfileDAO gameProfileDAO) {
		this.gameProfileDAO = gameProfileDAO;
	}

	public void setTagDAO(TagDAO tagDAO) {
		this.tagDAO = tagDAO;
	}

	@Override
	public GameVB getGameById(Long gameId) {
		Game g = gameDAO.load(gameId);
		return new GameVB(g);
	}

	@Override
	public Long addGame(String name, String description, Integer type,
			List<Long> tagIds, String url, String creator, String normalPic,
			String bigPic) {
		List<Tag> tags = tagDAO.getTagsByIds(tagIds);

		Game game = new Game();
		game.setName(name);
		game.setDescription(description);
		game.setType(type);
		game.setTags(tags);
		game.setUrl(url);
		game.setCreator(creator);
		game.setRegisterTime(new Date());
		game.setNormalPic(normalPic);
		game.setBigPic(bigPic);
		game.setDeleted(false);
		return gameDAO.save(game);
	}

	@Override
	public void deleteGame(Long gameId) {
		gameDAO.updateColumns(gameId, new String[] { "deleted" }, new Object[] { true });
	}

	@Override
	public void registerGame(Long gameId, Long id) {
		GameProfile profile = gameProfileDAO.getProfileByGameUser(gameId, id);
		if (profile == null) {
			profile = new GameProfile();

			Game game = new Game();
			game.setId(gameId);

			User user = new User();
			user.setId(id);

			profile.setUser(user);
			profile.setGame(game);
			profile.setHours(0);
			profile.setLevel(0);
			profile.setNullified(false);
			profile.setRegisterTime(new Date());
			gameProfileDAO.save(profile);
		} else {
			gameProfileDAO.updateColumns(profile.getId(),
					new String[] { "nullified" }, new Object[] { false });
		}

	}

	@Override
	public void nullifyGame(Long gameId, Long userId) {
		gameProfileDAO
				.updateByHql(
						"update GameProfile set nullified = ? where user.id= ? and game.id= ?",
						new Object[] { true, userId, gameId });

	}

	@Override
	public List<GameVB> listGame(Long id) {
		List<GameProfile> profiles = gameProfileDAO.getProfilesByUser(id);
		List<GameVB> games = new ArrayList<GameVB>();
		for (GameProfile p : profiles) {
			games.add(new GameVB(p.getGame()));
		}
		return games;
	}

	@Override
	public List<GameVB> findGame(String name, List<Long> tagIds,
			String description) {
		List<Game> games = gameDAO.findGames(name, tagIds, description);
		List<GameVB> result = new ArrayList<GameVB>();
		for (Game p : games) {
			result.add(new GameVB(p));
		}
		return result;
	}

}
